package com.applancer.nativecmsflip;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.applancer.dataprovider.DataProvider;
import com.applancer.form.CharacterField;
import com.applancer.form.FormActivity;
import com.applancer.form.NormalField;
import com.applancer.form.PasswordFieldRule;
import com.applancer.model.UserModel;
import com.applancer.provider.SystemBarTintManager;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class RegisterActivity extends FormActivity {

	protected EditText username;
	protected EditText password01;
	protected EditText password02;
	protected EditText email;
	protected EditText url;

	protected Button bt_register;
	protected DataProvider dataprovider;

	public static final String MYPREFERENCES = "RegisterRef";
	public static final String REG_USERNAME = "reg_username";
	public static final String REG_EMAIL = "reg_email";
	public static final String REG_URL = "reg_url";

	protected SharedPreferences sharedpreferences;
	protected Boolean registed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		initial();
		execution();
	}

	@Override
	protected void initializeFormFields() {

		username = (EditText) findViewById(R.id.register_user_name);
		password01 = (EditText) findViewById(R.id.register_pass);
		password02 = (EditText) findViewById(R.id.register_pass_retype);
		email = (EditText) findViewById(R.id.register_email);
		url = (EditText) findViewById(R.id.register_user_url);

		addValidator(new CharacterField(username));
		addValidator(new NormalField(password01));
		addValidator(new NormalField(password02));
		addValidator(new NormalField(email));
		addValidator(new NormalField(url));
		addValidator(new PasswordFieldRule(password01, password02));

	}

	private void initial() {
		bt_register = (Button) findViewById(R.id.register_user_register);
		dataprovider = new DataProvider(this);
		sharedpreferences = getSharedPreferences(MYPREFERENCES,
				Context.MODE_PRIVATE);
		registed = false;
	}

	private void execution() {

		initializeFormFields();

		getActionBar().setIcon(R.drawable.ic_detail_feed_back);
		getActionBar().setHomeButtonEnabled(true);

		bt_register.setOnClickListener(registerEvent);

		if (sharedpreferences.contains(REG_USERNAME)) {
			username.setText(sharedpreferences.getString(REG_USERNAME, ""));
		}

		if (sharedpreferences.contains(REG_EMAIL)) {
			email.setText(sharedpreferences.getString(REG_EMAIL, ""));
		}
		if (sharedpreferences.contains(REG_URL)) {
			url.setText(sharedpreferences.getString(REG_URL, ""));
		}

		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setNavigationBarTintEnabled(true);

		tintManager.setStatusBarTintResource(R.color.statusbar_bg);
		tintManager.setNavigationBarTintResource(R.color.actionbar_bg);

	}

	OnClickListener registerEvent = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (!validateForm()) {
				return;
			}

			registerUser();
		}

	};

	private void registerUser() {

		UserModel user = new UserModel();
		user.setUserName(username.getText().toString());
		user.setPass(password02.getText().toString());
		user.setEmail(email.getText().toString());
		user.setUrl(url.getText().toString());

		dataprovider.register(user, handler);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			onBackPressed();
			break;

		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();

		if (registed == false) {
			Editor editor = sharedpreferences.edit();
			editor.putString(REG_USERNAME, username.getText().toString());
			editor.putString(REG_EMAIL, email.getText().toString());
			editor.putString(REG_URL, url.getText().toString());

			editor.commit();
		}
		overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
	}

	AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] response) {
			String result = new String(response);

			try {
				JSONObject jsonObj = new JSONObject(result);
				String str = jsonObj.getString("results");

				Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT)
						.show();
				registed = true;
				onBackPressed();

			} catch (Exception e) {

			}

		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};

}
